package com.qst.express.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qst.express.entity.Courier;
import com.qst.express.entity.vo.CourierVo;
import com.qst.express.mapper.CourierMapper;
import com.qst.express.service.CourierService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjh
 * @since 2022-09-25
 */
@Service
public class CourierServiceImpl extends ServiceImpl<CourierMapper, Courier> implements CourierService {
    /**
     * 用于查询数据库中的全部快递员（总数+日注册量）
     *
     * @return [{size:总数,day:新增}]
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    @Override
    public List<Map<String, String>> console() {
        CourierVo courierTotal = baseMapper.getCourierTotal();

        ArrayList<Map<String, String>> data = new ArrayList<>();
        Map data1 = new HashMap();
        data1.put("data_size", courierTotal.getDataSize().toString());
        data1.put("data_day", courierTotal.getDataDay().toString());
        data.add(data1);

        return data;
    }

    /**
     * 快递员分页查询
     * @param courierPage
     * @param courierVo
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    @Override
    public IPage<Courier> pageTeacherCondition(Page<Courier> courierPage, CourierVo courierVo) {
        QueryWrapper<Courier> queryWrapper = new QueryWrapper<>();
        if (courierVo != null) {
            String cname = courierVo.getCname();
            String cphone = courierVo.getCphone();
            String begin = courierVo.getBegin();
            String end = courierVo.getEnd();
            if (!StringUtils.isEmpty(cname)) {
                queryWrapper.like("cname", cname);
            }
            if (!StringUtils.isEmpty(cphone)) {
                queryWrapper.like("cphone", cphone);
            }
            if (!org.springframework.util.StringUtils.isEmpty(begin)) {
                queryWrapper.ge("createtime", begin);
            }
            if (!org.springframework.util.StringUtils.isEmpty(end)) {
                queryWrapper.le("createtime", end);
            }
        }
        IPage<Courier> eduTeacherIPage = this.baseMapper.selectPage(courierPage, queryWrapper);
        return courierPage;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public boolean isCourierByPhone(String phone) {
        Courier courier = baseMapper.findByPhone(phone);
        System.out.println(courier);
        if (courier == null) {
            return false;
        } else {
            return true;
        }
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public void updateLoginTime(String userPhone) {
        baseMapper.updateLoginTime(userPhone);
    }

    //根据手机号查询快递员信息
    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public Courier findCourierByPhone(String userPhone) {
        QueryWrapper<Courier> wrapper = new QueryWrapper<Courier>();
        wrapper.eq("cphone", userPhone);
        Courier courier = baseMapper.selectOne(wrapper);
        return courier;
    }
    @Transactional
    @Override
    public boolean isIdentification(String phone) {
        QueryWrapper<Courier> wrapper = new QueryWrapper<Courier>();
        wrapper.eq("cphone", phone);
        Courier courier = baseMapper.selectOne(wrapper);
        if (courier.getIdentification() == 0) {
            return false;
        } else {
            return true;
        }
    }
    @Transactional
    @Override
    public boolean updateCourier(Courier c) {
        QueryWrapper<Courier> wrapper = new QueryWrapper<Courier>();
        wrapper.eq("cphone", c.getCphone());
        c.setIdentification(1);
        int update = baseMapper.update(c, wrapper);
        if (update > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    @Transactional
    public boolean updateUserName(String userPhone, String userName) {
        QueryWrapper<Courier> wrapper = new QueryWrapper<Courier>();
        Courier c = new Courier();
        c.setCname(userName);
        wrapper.eq("cphone", userPhone);
        int update = baseMapper.update(c, wrapper);
        return update>0?true:false;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public boolean addDelivery(String userphone) {
        int update = baseMapper.updateDelivery(userphone);

        return update>0?true:false;
    }


}
